package com.ss.android.article.base.feature.feed.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.feed.c.FilterWord;
import com.ss.android.article.base.feature.feed.dd;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.feature.video.DialogShowHelper;
import com.ss.android.article.base.ui.BaseDialog;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.WeakHashMap;
import org.json.JSONException;
import org.json.JSONObject;

public final class DislikeDialog extends BaseDialog implements dd {
    public final class a extends BaseAdapter {
        private List a;
        private LayoutInflater b;
        private Resources c;
        private View.OnClickListener d;

        public a(Context arg3, List arg4, View.OnClickListener arg5) {
            int v1 = 6;
            this.b = LayoutInflater.from(arg3);
            AppData.getInstance();
            AppData.isNightMode();
            this.c = arg3.getResources();
            this.d = arg5;
            this.a = arg4.size() > v1 ? arg4.subList(0, v1) : arg4;
        }

        public final int getCount() {
            return this.a.size();
        }

        public final Object getItem(int arg2) {
            return this.a.get(arg2);
        }

        public final long getItemId(int arg3) {
            return 0;
        }

        public final View getView(int arg7, View arg8, ViewGroup arg9) {
            boolean v3 = false;
            Object v0 = this.a.get(arg7);
            View v1 = this.b.inflate(com.ss.android.article.browser.R.layout.cu, arg9, false);
            View v2 = ((ViewGroup)v1).getChildAt(0);
            ((TextView)v2).setText(((FilterWord)v0).b);
            String v4 = ((FilterWord)v0).a;
            if(!TextUtils.isEmpty(((CharSequence)v4)) && DislikeDialog.c != null) {
                v3 = DislikeDialog.c.containsKey(v4);
            }

            ((TextView)v2).setSelected(v3);
            ((TextView)v2).setClickable(true);
            ((TextView)v2).setOnClickListener(this.d);
            ((TextView)v2).setTag(v0);
            ((TextView)v2).setTextColor(this.c.getColorStateList(com.ss.android.article.browser.R.color.y7));
            UIUtils.setBackgroundResource(v2, R.drawable.e1);
            return v1;
        }
    }

    public WindowFocusChangeListener a;
    public com.ss.android.article.base.feature.feed.dd.a b;
    static WeakHashMap c;
    private DislikeRelativeLayout d;
    private Context e;
    private Resources f;
    private LayoutInflater g;
    private CellRef h;
    private ImageView i;
    private LinearLayout j;
    private TextView k;
    private TextView l;
    private GridView n;
    private ImageView o;
    private boolean p;
    private List q;
    private String r;
    private static String s;
    private View.OnClickListener t;
    private View.OnClickListener u;

    static {
        DislikeDialog.c = new WeakHashMap();
        DislikeDialog.s = "";
    }

    public DislikeDialog(Activity arg6, CellRef arg7, String arg8) {
        super(arg6, R.style.jz);
        this.p = false;
        this.q = new ArrayList();
        this.t = new View.OnClickListener() {
            public final void onClick(View arg5) {
                try {
                    if(arg5.getId() != R.id.title_ok_btn) {
                        return;
                    }

                    e();
                    if(b == null) {
                        return;
                    }

                    b.a.onDislikeClick(true);
                }
                catch(Throwable v0) {
                    Logger.e("DislikeDialog", "error occurs in DislikeDialog.clickedListener, " + v0.toString());
                }
            }
        };
        this.u = new View.OnClickListener() {
            public final void onClick(View arg3) {
                DislikeDialog.a(arg3);
                d(DislikeDialog.c.size());
            }
        };
        this.e = ((Context)arg6);
        this.h = arg7;
        this.r = arg8;
        this.f = arg6.getResources();
        this.g = (LayoutInflater) arg6.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.d = (DislikeRelativeLayout) this.g.inflate(R.layout.dislike_dialog_layout, null);
        if(this.d != null) {
            this.i = this.d.findViewById(com.ss.android.article.browser.R.id.top_arrow);
            this.j = this.d.findViewById(com.ss.android.article.browser.R.id.main_layout);
            this.k = this.d.findViewById(com.ss.android.article.browser.R.id.dislike_dialog_title);
            this.n = this.d.findViewById(com.ss.android.article.browser.R.id.dislike_item_gridview);
            this.l = this.d.findViewById(com.ss.android.article.browser.R.id.title_ok_btn);
            this.o = this.d.findViewById(com.ss.android.article.browser.R.id.bottom_arrow);
            this.d.a(this.j);
        }

        this.l.setOnClickListener(this.t);
        this.setContentView(this.d);
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
        Window v0 = this.getWindow();
        WindowManager.LayoutParams v1 = v0.getAttributes();
        v0.setGravity(53);
        v1.x = 0;
        v1.y = 0;
        v1.width = -1;
        v1.height = -2;
        v1.alpha = 1f;
        v1.windowAnimations = 0;
        v1.dimAmount = 0.3f;
        v0.setAttributes(v1);
        List v0_1 = arg7.filterWordList;
        String v1_1 = arg7.cellId;
        if(this.e != null && this.n != null && !android.support.design.a.isTextEmpty(v1_1)) {
            if(!v1_1.equals(DislikeDialog.s)) {
                DislikeDialog.c.clear();
                DislikeDialog.s = v1_1;
            }

            if(this.q != null) {
                this.q.clear();
            }
            else {
                this.q = new ArrayList(v0_1.size());
            }

            this.d();
            this.q.addAll(((Collection)v0_1));
            this.n.setAdapter(new a(this.mActivity, this.q, this.u));
        }

        this.d.setCallback(new DislikeRelativeLayout.a() {
            public final void a() {
                e();
            }
        });
        if(!arg7.filterWordList.isEmpty()) {
            this.a(((Context)arg6), "menu_with_reason");
        }
        else {
            this.a(((Context)arg6), "menu_no_reason");
        }
    }

    private void a(Context arg10, String arg11) {
        long v6 = 0;
        JSONObject v8 = new JSONObject();
        try {
            v8.putOpt("category_name", this.r);
        }
        catch(JSONException v0) {
            v0.printStackTrace();
        }

        String v2 = "dislike";
        long v4 = this.h.article != null ? this.h.article.groupId : v6;
        MobClickCombiner.onEvent(arg10, v2, arg11, v4, v6, v8);
    }

    static void a(View arg6) {
        boolean v3 = true;
        if(arg6 != null) {
            Object v1 = arg6.getTag();
            if((v1 instanceof FilterWord)) {
                Object v0 = v1;
                boolean v5 = arg6.isSelected();
                boolean v2 = !v5 ? true : false;
                arg6.setSelected(v2);
                if(v5) {
                    v3 = false;
                }

                ((FilterWord)v0).c = v3;
                if(android.support.design.a.isTextEmpty(((FilterWord)v0).a)) {
                    return;
                }

                if(v5) {
                    DislikeDialog.c.remove(((FilterWord)v0).a);
                    return;
                }

                DislikeDialog.c.put(((FilterWord)v0).a, v1);
            }
        }
    }

    private boolean a(boolean arg13, Animation.AnimationListener arg14) {
        int v10;
        int v9;
        boolean v0;
        int v3 = this.d.getMeasuredWidth();
        int v4 = this.d.getMeasuredHeight();
        if(!this.c()) {
            v0 = false;
        }
        else {
            float v6 = 0f;
            float v8 = 0f;
            if(this.i.getVisibility() != View.VISIBLE || !(this.i.getLayoutParams() instanceof ViewGroup.MarginLayoutParams)) {
                if(this.o.getVisibility() == View.VISIBLE && ((this.o.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))) {
                    v6 = 1f - (((float)(((ViewGroup.MarginLayoutParams)this.o.getLayoutParams()).rightMargin + this.o.getWidth() / 3))) / (((float)v3));
                    v8 = 1f;
                    v9 = 0;
                    v10 = v4 / 4;
                }else {
                    v9 = 0;
                    v10 = 0;
                }
            } else {
                v6 = 1f - (((float)(((ViewGroup.MarginLayoutParams)this.i.getLayoutParams()).rightMargin + this.i.getWidth() / 3))) / (((float)v3));
                v8 = 0f;
                v9 = v4 / 4;
                v10 = 0;
            }

            if(v6 < 0f) {
                v6 = 0f;
            }
            else if(v6 > 1f) {
                v6 = 1f;
            }

            AnimationSet v11 = new AnimationSet(false);
            float v1 = arg13 ? 0f : 1f;
            float v2 = arg13 ? 1f : 0f;
            float v3_1 = arg13 ? 0f : 1f;
            float v4_1 = arg13 ? 1f : 0f;
            ScaleAnimation v0_1 = new ScaleAnimation(v1, v2, v3_1, v4_1, 1, v6, 1, v8);
            v0_1.setInterpolator(new OvershootInterpolator(1.5f));
            v0_1.setAnimationListener(arg14);
            v0_1.setDuration(300);
            if(!arg13) {
                AlphaAnimation v1_1 = new AlphaAnimation(1f, 0f);
                v1_1.setInterpolator(new DecelerateInterpolator(3f));
                v1_1.setDuration(300);
                v11.addAnimation(((Animation)v1_1));
            }

            v11.addAnimation(((Animation)v0_1));
            this.d.clearAnimation();
            this.d.startAnimation(((Animation)v11));
            if(arg13) {
                this.d.setPadding(this.d.getPaddingLeft(), v10, this.d.getPaddingRight(), v9);
            }

            v0 = true;
        }

        return v0;
    }

    public final void a() {
        this.a = null;
    }

    public final void a(int arg3) {
        ViewGroup.LayoutParams v0 = this.i.getLayoutParams();
        ((ViewGroup.MarginLayoutParams)v0).rightMargin = arg3;
        this.i.setLayoutParams(v0);
    }

    public final void a(boolean arg5) {
        int v2 = 8;
        ImageView v3 = this.i;
        int v0 = arg5 ? 0 : v2;
        UIUtils.setVisibility(((View)v3), v0);
        ImageView v0_1 = this.o;
        if(!arg5) {
            v2 = 0;
        }

        UIUtils.setVisibility(((View)v0_1), v2);
        this.d.invalidate();
    }

    public final void b(boolean arg4) {
        if(this.p != arg4) {
            this.p = arg4;
            this.i.setImageResource(com.ss.android.article.browser.R.drawable.arrow_up_popup_textpage);
            UIUtils.setBackgroundResource(this.j, R.drawable.dislike_dialog_bg);
            this.k.setTextColor(this.f.getColor(com.ss.android.article.browser.R.color.ssxinzi2));
            UIUtils.setMaxLines(this.l, R.drawable.dislike_title_btn_background_selector);
            this.l.setTextColor(this.f.getColor(com.ss.android.article.browser.R.color.ssxinzi7));
            this.o.setImageResource(com.ss.android.article.browser.R.drawable.arrow_down_popup_textpage);
        }
    }

    public final int b() {
        return this.d.getMeasuredHeight();
    }

    public final void b(int arg3) {
        ViewGroup.LayoutParams v0 = this.o.getLayoutParams();
        ((ViewGroup.MarginLayoutParams)v0).rightMargin = arg3;
        this.o.setLayoutParams(v0);
    }

    public final boolean c() {
        boolean v0 = this.d.getMeasuredWidth() <= 0 || this.d.getMeasuredHeight() <= 0 ? false : true;
        return v0;
    }

    public final void c(int arg4) {
        Window v0 = this.getWindow();
        WindowManager.LayoutParams v1 = v0.getAttributes();
        v0.setGravity(53);
        v1.x = 0;
        v1.y = arg4;
        v0.setAttributes(v1);
        this.show();
        this.a(true, null);
    }

    public final void cancel() {
        long v4;
        String v3;
        String v2;
        Context v1;
        long v6 = 0;
        this.e();
        JSONObject v8 = new JSONObject();
        try {
            v8.putOpt("category_name", this.r);
        }
        catch(JSONException v0) {
            v0.printStackTrace();
        }

        if(!this.h.filterWordList.isEmpty()) {
            v1 = this.e;
            v2 = "cancel_dislike";
            v3 = "cancel_dislike_with_reason";
            v4 = this.h.article != null ? this.h.article.groupId : v6;
            MobClickCombiner.onEvent(v1, v2, v3, v4, v6, v8);
        }
        else {
            v1 = this.e;
            v2 = "cancel_dislike";
            v3 = "cancel_dislike_no_reason";
            v4 = this.h.article != null ? this.h.article.groupId : v6;
            MobClickCombiner.onEvent(v1, v2, v3, v4, v6, v8);
        }
    }

    public final void d() {
        int v5 = 11;
        this.d(DislikeDialog.c.size());
        ViewGroup.LayoutParams v0 = this.j.getLayoutParams();
        int v1 = this.f.getDimensionPixelSize(R.dimen.dw);
        if(UIUtils.getScreenWidth(this.e) > this.f.getDimensionPixelSize(R.dimen.list_item_horizontal_outside_padding) * 2 + v1) {
            ((RelativeLayout.LayoutParams)v0).addRule(v5);
            ((RelativeLayout.LayoutParams)v0).width = v1;
        }
        else {
            ((RelativeLayout.LayoutParams)v0).addRule(v5);
            ((RelativeLayout.LayoutParams)v0).addRule(9);
            ((RelativeLayout.LayoutParams)v0).width = -1;
        }

        this.j.setLayoutParams(v0);
    }

    final void d(int arg7) {
        String v0;
        if(this.f != null && this.k != null && this.l != null) {
            if(arg7 > 0) {
                v0 = this.f.getString(com.ss.android.article.browser.R.string.dislike_dlg_btn_ok);
                SpannableString v2 = new SpannableString(String.format(this.e.getResources().getString(com.ss.android.article.browser.R.string.dislike_dlg_title_with_selected_items), Integer.valueOf(arg7)));
                v2.setSpan(new ForegroundColorSpan(this.e.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi4)), 2, 3, 34);
                this.k.setText(((CharSequence)v2));
            }
            else {
                v0 = this.e.getResources().getString(com.ss.android.article.browser.R.string.dislike_dlg_btn_delete);
                this.k.setText(this.e.getResources().getString(com.ss.android.article.browser.R.string.dislike_dlg_title_no_selected_items));
            }

            this.l.setText(((CharSequence)v0));
        }
    }

    public final void dismiss() {
        super.dismiss();
        DialogShowHelper.getInst().removeDialog(this);
    }

    public final void e() {
        if((this.isShowing()) && (this.g()) && !this.a(false, new Animation.AnimationListener() {

            public final void onAnimationEnd(Animation arg2) {
                if((isShowing()) && (g())) {
                    dismiss();
                }
            }

            public final void onAnimationRepeat(Animation arg1) {
            }

            public final void onAnimationStart(Animation arg1) {
            }
        }) && (this.isShowing())) {
            this.dismiss();
        }
    }

    public final void onWindowFocusChanged(boolean arg2) {
        if(this.a != null) {
            this.a.a(this);
        }

        this.b(this.p);
    }

    public final void show() {
        super.show();
        DialogShowHelper.getInst().addDialog(this);
    }
}

